// components/ListRemote/index.js
const App = getApp();
Component({
    options: {
        multipleSlots: true // 在组件定义时的选项中启用多slot支持
    },
    /**
     * 组件的属性列表
     */
    properties: {
        jsonbody: Object,
        curl: String,
        pagetion: {
            type: Object,
            value: {
                page: 1,
                pagesize: 10,
                u: true
            }
        },
        item: Object,
        height: {
            type: String,
            value: "100vh"
        },
        emptyText: {
            type: String,
            value: "暂无数据"
        },
        "refresher-enabled": {
            type: Boolean,
            value: true
        },
        /* 剪掉的高度 */
        "deleteHeight": {
            type: Number,
            value: 0
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        refresherStyle: { p: 1, t: `scale(1)` },
        list: [],
        listLength: 0,
        triggered: false,
        loadend: false,
        showDataEnd: false,
        showEmpty: false,
        scrollTop: 0
    },
    observers: {
        curl(v) {
            if (!v) return;
            let { deleteHeight } = this.data;
            let sys = wx.getSystemInfoSync();
            let { windowHeight } = sys;
            this.setData({
                height: `${windowHeight - deleteHeight}px`,
            });
            this.getList(true)
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        async onRefresh(e) {
            if (this.data.triggered) return;
            // this.setData({ triggered: true, "pagetion.page": 1 });
            await this.getList(true);
        },
        onPulling(evt) {
            var p = Math.min(evt.detail.dy / 80, 1);
            this.setData({
                refresherStyle: { p, t: `scale(${p})` }
            });
        },
        onScrolltolower() {
            if (this.data.loadend) return;
            let { page } = this.data.pagetion;
            page++;
            this.setData({ "pagetion.page": page });
            this.getList(false);
        },
        async getList(init) {
            try {
                if (init) {
                    wx.pageScrollTo({ scrollTop: 0, duration: 100 });
                    this.setData({ scrollTop: 0, showEmpty: false, showDataEnd: false });
                }
                App.showLoading();
                let { jsonbody, pagetion, curl, showEmpty, showDataEnd } = this.data;
                let { page } = pagetion;
                page = init === true ? 1 : page;
                // console.log('page: ', page);
                let res = await App.$http(curl, { ...jsonbody, ...pagetion, page });
                // console.log(res)
                if (page === 1 && !res.data.length) {
                    showEmpty = true;
                }
                if (page > 1 && !res.data.length) {
                    showDataEnd = true;
                }
                this.setList(init, res.data);
                this.setData({ triggered: false, "pagetion.page": page, showEmpty, showDataEnd,res });
                if (init) {
                    this.triggerEvent("refresh");
                }
            } catch (e) {
                console.warn(e)
            } finally {
                wx.hideLoading();
            }
        },
        /**
         * @method
         * @param {Boolean} initType 初始化为true，加载为false
         * list 初始/加载的数据
         * this指向Vue对象
         * loadend 数据库加载完毕
         * loadOK 请求完毕
         */
        setList(initType, list) {
            if (initType) {
                this.setData({
                    list: list.length ? [list] : [],
                    listLength: list.length ? 1 : 0,
                    loadend: false
                });
                return;
            }
            if (!list.length) {
                this.setData({
                    loadend: true
                });
                return;
            }
            let len = this.data.list.length;
            this.setData({
                [`list[${len}]`]: list,
                listLength: len + 1
            });
        },
        triggerFn(e) {
            let { fn, data } = e.detail;
            this.triggerEvent(fn, data);
        },
        setChildData(obj) {
            this.selectComponent("#child").setData(obj);
        },
        updateItem(obj = {}) {
            let { pid, cid, nitem } = obj;
            let { list } = this.data;
            let oItem = list[pid][cid];
            Object.keys(nitem).forEach((key) => {
                if (oItem[key] !== undefined) {
                    oItem[key] = nitem[key]
                }
            });
            this.setData({
                [`list[${pid}][${cid}]`]: oItem
            });
        },
        deleteItem(obj = {}) {
            let { pid, cid, nitem } = obj;
            //let { list } = this.data;
            this.setData({
                [`list[${pid}][${cid}]`]: {}
            });
        }
    }
})